'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.Data

Imports System.IO

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports JCsTools.IdentityManager



'''<summary>
'''This is a test class for XmlIdentityProviderTest and is intended
'''to contain all XmlIdentityProviderTest Unit Tests
'''</summary>
<TestClass()> _
Public Class XmlIdentityProviderTest


    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

#Region "Additional test attributes"
    '
    'You can use the following additional attributes as you write your tests:
    '
    'Use ClassInitialize to run code before running the first test in the class
    '<ClassInitialize()>  _
    'Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    'End Sub
    '
    'Use ClassCleanup to run code after all tests in a class have run
    '<ClassCleanup()>  _
    'Public Shared Sub MyClassCleanup()
    'End Sub
    '
    'Use TestInitialize to run code before running each test
    '<TestInitialize()>  _
    'Public Sub MyTestInitialize()
    'End Sub
    '
    'Use TestCleanup to run code after each test has run
    '<TestCleanup()>  _
    'Public Sub MyTestCleanup()
    'End Sub
    '
#End Region


    '''<summary>
    '''A test for Save
    '''</summary>
    <TestMethod()> _
    Public Sub SaveTest()
        Dim xmlFile As FileInfo = New FileInfo(Path.GetTempFileName)
        Dim target As XmlIdentityProvider = New XmlIdentityProvider(xmlFile)

        Dim id1 As IIdentity = New Identity("Jan-Cornelius Molnar")

        id1.Description = "Test String 1"
        id1.ImageUrl = "D:\Pictures\pony.jpg"
        id1.SetAttribute("Password", "blank")
        id1.SetAttribute("UIN", "148372642")

        target.Identities.Add(id1)

        Dim id2 As IIdentity = New Identity("123456")

        id2.Description = "Test String 2"
        id2.ImageUrl = "D:\Pictures\pony_small.jpg"
        id2.SetAttribute("Password", "geheim")
        id2.SetAttribute("UIN", "123456")

        target.Identities.Add(id2)

        target.Save()

        Assert.IsTrue(xmlFile.Length > 0, "No data was written.")

        target = New XmlIdentityProvider(xmlFile)

        target.Load()

        Dim identities = target.Identities

        Assert.AreEqual(identities.Count, 2, "There should be 2 Identities in the list.")

        id1 = (From x In identities Where x.Identifier = "Jan-Cornelius Molnar").FirstOrDefault

        Assert.IsNotNull(id1, "Identity 'Jan-Cornelius Molnar' was not loaded.")

        Assert.AreEqual(id1.Description, "Test String 1", "Description was not loaded.")
        Assert.AreEqual(id1.ImageUrl, "D:\Pictures\pony.jpg", "ImageUrl was not loaded.")

        Dim uinValue As String = id1.GetAttribute("UIN")
        Dim passwordValue As String = id1.GetAttribute("Password")

        Assert.AreEqual(uinValue, "148372642", "UIN Attribute was not loaded.")
        Assert.AreEqual(passwordValue, "blank", "Password Attribute was not loaded.")
    End Sub

    '''<summary>
    '''A test for Load
    '''</summary>
    <TestMethod()> _
    Public Sub LoadTest()
        Dim xdocument = <?xml version="1.0" standalone="yes"?>
                        <IdentityData>
                            <Identities>
                                <Id>b93e1c5f-b92d-4f89-82a1-2b9e1b2428d0</Id>
                                <Identifier>123456</Identifier>
                                <ImageUrl>D:\Pictures\pony_small.jpg</ImageUrl>
                                <Description>Test String 2</Description>
                            </Identities>
                            <Identities>
                                <Id>b28729bf-a90e-4397-a4ea-7cdfd36baa60</Id>
                                <Identifier>Jan-Cornelius Molnar</Identifier>
                                <ImageUrl>D:\Pictures\pony.jpg</ImageUrl>
                                <Description>Test String 1</Description>
                            </Identities>
                            <Attributes>
                                <Id>a2ed8862-26f3-4da5-bb57-672a6a9d1907</Id>
                                <IdentityId>b93e1c5f-b92d-4f89-82a1-2b9e1b2428d0</IdentityId>
                                <Key>UIN</Key>
                                <Value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123456</Value>
                            </Attributes>
                            <Attributes>
                                <Id>4ee27193-302e-4ee5-81d9-57b1f822b61c</Id>
                                <IdentityId>b93e1c5f-b92d-4f89-82a1-2b9e1b2428d0</IdentityId>
                                <Key>Password</Key>
                                <Value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">geheim</Value>
                            </Attributes>
                            <Attributes>
                                <Id>7a84c3e0-e6c9-4069-ab59-a699bf48f754</Id>
                                <IdentityId>b28729bf-a90e-4397-a4ea-7cdfd36baa60</IdentityId>
                                <Key>UIN</Key>
                                <Value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">148372642</Value>
                            </Attributes>
                            <Attributes>
                                <Id>7e195eb9-3846-4fd6-8069-303a456688b0</Id>
                                <IdentityId>b28729bf-a90e-4397-a4ea-7cdfd36baa60</IdentityId>
                                <Key>Password</Key>
                                <Value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">blank</Value>
                            </Attributes>
                        </IdentityData>

        Dim xmlFile As FileInfo = New FileInfo(Path.GetTempFileName)

        xdocument.Save(xmlFile.FullName)

        Dim target As XmlIdentityProvider = New XmlIdentityProvider(xmlFile)
        target.Load()

        Dim identities = target.Identities

        Assert.AreEqual(identities.Count, 2, "There should be 2 Identities in the list.")

        Dim id1 = (From x In identities Where x.Identifier = "Jan-Cornelius Molnar").FirstOrDefault

        Assert.IsNotNull(id1, "Identity 'Jan-Cornelius Molnar' was not loaded.")

        Assert.AreEqual(id1.Description, "Test String 1", "Description was not loaded.")
        Assert.AreEqual(id1.ImageUrl, "D:\Pictures\pony.jpg", "ImageUrl was not loaded.")

        Dim uinValue As String = id1.GetAttribute("UIN")
        Dim passwordValue As String = id1.GetAttribute("Password")

        Assert.AreEqual(uinValue, "148372642", "UIN Attribute was not loaded.")
        Assert.AreEqual(passwordValue, "blank", "Password Attribute was not loaded.")
    End Sub
End Class
